package com.xbongbong.crm.perform.query.detail;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmDetailTopInfoService;
import com.xbongbong.crm.handle.analysis.CommunicatePlanDetailAnalysisDataHandler;
import com.xbongbong.crm.service.permission.CommunicatePlanPermissionServiceImpl;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.call.CallCenterService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkListPojo;
import com.xbongbong.paas.help.CloudCodeHelp;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.pojo.ButtonPojo;
import com.xbongbong.paas.pojo.CustomerUserPojo;
import com.xbongbong.paas.pojo.DataDetailPojo;
import com.xbongbong.paas.pojo.DetaPhonePojo;
import com.xbongbong.paas.pojo.FormTitlePoJo;
import com.xbongbong.paas.pojo.HandelDetailPojo;
import com.xbongbong.paas.pojo.SummaryDataPoJo;
import com.xbongbong.paas.pojo.vo.FormDataGetVO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.service.BusinessButtonService;
import com.xbongbong.paas.service.LinkListService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.paas.util.FormUtil;
import com.xbongbong.paas.util.TitleUtil;
import com.xbongbong.parent.parse.data.FormDataGetAnalysisDataHelp;
import com.xbongbong.pro.appdetail.pojo.MobileDetailDirectBlockPojo;
import com.xbongbong.pro.appdetail.pojo.MobileDetailExecutorBlockPojo;
import com.xbongbong.pro.appdetail.pojo.MobileDetailMoneyItemPojo;
import com.xbongbong.pro.appdetail.pojo.MobileDetailScriptPojo;
import com.xbongbong.pro.appdetail.pojo.MobileDetailStatusPojo;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.form.pojo.TopPermissionsPoJo;
import com.xbongbong.pro.formdata.pojo.dto.FormDataDetailDTO;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formdata.pojo.vo.MobileFormCommonDetailVO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.CommunicatePlanUserEntity;
import com.xbongbong.saas.enums.BusinessDetailButtonEnum;
import com.xbongbong.saas.enums.MobileDetailBottomOperationEnum;
import com.xbongbong.saas.enums.SaasButtonEnum;
import com.xbongbong.saas.enums.WorkFlowEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.dictionary.CommunicatePlanStatusEnum;
import com.xbongbong.saas.enums.dictionary.DictionaryEnum;
import com.xbongbong.saas.model.CommunicatePlanModel;
import com.xbongbong.saas.model.CommunicatePlanUserModel;
import com.xbongbong.saas.model.DataDictionaryModel;
import com.xbongbong.saas.service.toolbox.help.UserTeamHelp;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import com.xbongbong.util.NameUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author sunshaoxiang
 * @ClassName CommunicatePlanDetailTopInfoServiceImpl
 * @Description 访客计划详情页 top info 业务层
 * @Date 2022/5/24 10:17
 * @Version 1.0
 **/
@Service("communicatePlanDetailTopInfoServiceImpl")
public class CommunicatePlanDetailTopInfoServiceImpl extends AbstractCrmDetailTopInfoService {

    public static final Logger LOGGER = LoggerFactory.getLogger(CommunicatePlanDetailTopInfoServiceImpl.class);

    @Resource
    private CommunicatePlanUserModel communicatePlanUserModel;
    @Resource
    private UserModel userModel;
    @Resource
    private CloudCodeHelp cloudCodeHelp;
    @Resource
    private FormDataGetAnalysisDataHelp formDataGetAnalysisDataHelp;
    @Resource
    private CallCenterService callCenterService;
    @Resource
    private LinkListService linkListService;
    @Resource
    private CommunicatePlanModel communicatePlanModel;
    @Resource
    private CommunicatePlanDetailAnalysisDataHandler communicatePlanDetailAnalysisDataHandler;
    @Resource
    private DataDictionaryModel dataDictionaryModel;
    @Resource
    private UserTeamHelp userTeamHelp;
    @Resource
    private BusinessButtonService businessButtonService;
    @Resource
    private CommunicatePlanPermissionServiceImpl communicatePlanPermissionService;
    @Resource
    private PaasMenuModel paasMenuModel;

    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.COMMUNICATE_PLAN.getCode());
    }

    @Override
    public List<SummaryDataPoJo> getSummaryList(FormDataDetailDTO formDataDetailDTO, PaasFormDataEntityExt paasFormDataEntityExt, PaasFormEntityExt paasFormEntityExt, List<FieldAttrEntity> list, Map<String, FieldAttrEntity> explainMap, JSONObject data) throws XbbException {
        return ExplainUtil.getSpecialSummaryList(paasFormEntityExt.getSummaryList(), data, explainMap, paasFormDataEntityExt);
    }

    @Override
    public List<SummaryDataPoJo> getLabelList(FormDataDetailDTO formDataDetailDTO, PaasFormDataEntityExt paasFormDataEntityExt, PaasFormEntityExt paasFormEntityExt, List<FieldAttrEntity> list, Map<String, FieldAttrEntity> explainMap, JSONObject data) throws XbbException {
        return ExplainUtil.getSpecialSummaryList(paasFormEntityExt.getLabelsList(), data, explainMap, paasFormDataEntityExt);
    }

    @Override
    public List<FormTitlePoJo> getTitleList(FormDataDetailDTO formDataDetailDTO, PaasFormDataEntityExt formData, PaasFormEntityExt formEntity, List<FieldAttrEntity> list, Map<String, FieldAttrEntity> explainMap, JSONObject data) throws XbbException {
        return ExplainUtil.getSpecailFormTitle(JSONArray.parseArray(formEntity.getCustomTitle()), explainMap, data, list, formData);
    }

    @Override
    public List<MobileDetailMoneyItemPojo> getMobileDetailMoneyInfo(Integer distributorMark, String corpid, String userId, Integer businessType, Integer subBusinessType, Long dataId, JSONObject data, Map<String, FieldAttrEntity> explainMap, UserVO loginUser, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        return new ArrayList<>();
    }

    @Override
    public JSONArray getBottomBlockInfo(String corpid, String userId, Integer businessType, PaasFormEntityExt formEntity, Long dataId, JSONObject data, Map<String, FieldAttrEntity> explainMap, PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO) throws XbbException {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<String, UserEntity> userMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<UserEntity> userEntityList;
        List<CustomerUserPojo> userPojoList = new ArrayList<>();
        List<String> userIdIn = new ArrayList<>();
        userIdIn.add("-1");

        Integer editAfterFinish = 0;
        JSONArray bottomBlockInfo = new JSONArray();
        Long communicatePlanCustomerId = FastJsonHelper.getLongOrDefaultFromFormData(paasFormDataEntityExt.getData(), CommunicatePlanEnum.LINK_CUSTOMER.getAttr(), 0L);
        String communicatePlanCustomerName = FastJsonHelper.getStringOrDefaultFromFormData(paasFormDataEntityExt.getData(), CommunicatePlanEnum.CUSTOMER_NAME_LINK_TEXT.getAttr(), "");
        bottomBlockInfo.add(new MobileDetailDirectBlockPojo(communicatePlanCustomerId, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), communicatePlanCustomerName));

        param.put("corpid", corpid);
        param.put("del", 0);
        param.put("planId", dataId);
        List<CommunicatePlanUserEntity> planUserList = communicatePlanUserModel.findEntitys(param);
        planUserList.forEach(item -> userIdIn.add(item.getUserId()));
        userEntityList = userModel.getByUserIdIn(corpid, userIdIn, true);
        userEntityList.forEach(item -> userMap.put(item.getUserId(), item));

        // 对拥有相同 UserId 的CommunicatePlanUserEntity队列进行排重
        Set<String> userIdSet = new HashSet<>();
        Iterator<CommunicatePlanUserEntity> userEntityIterator = planUserList.iterator();
        while (userEntityIterator.hasNext()) {
            CommunicatePlanUserEntity userEntity = userEntityIterator.next();
            if (userIdSet.contains(userEntity.getUserId())) {
                userEntityIterator.remove();
            } else {
                userIdSet.add(userEntity.getUserId());
            }
        }
        planUserList.forEach(item -> {
            UserEntity userEntity = userMap.get(item.getUserId());
            if (userEntity != null) {
                CustomerUserPojo customerUserPojo = new CustomerUserPojo();
                customerUserPojo.setUserId(userEntity.getUserId());
                customerUserPojo.setName(userEntity.getName());
                customerUserPojo.setAvatar(userEntity.getAvatar());
                userPojoList.add(customerUserPojo);
            }
        });
        bottomBlockInfo.add(new MobileDetailExecutorBlockPojo(userPojoList));
        return bottomBlockInfo;
    }

    @Override
    public List<MobileDetailScriptPojo> getDetailShowScript(Integer businessType, UserVO loginUser, PaasFormDataEntityExt paasFormDataEntityExt, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        return new ArrayList<>();
    }

    @Override
    public MobileDetailStatusPojo getDetailShowStatus(Integer businessType, JSONObject data) throws XbbException {
        return null;
    }

    @Override
    public Map<String, Object> getDetailShowOperation(String corpid, String userId, Integer businessType, Long dataId, JSONObject data) throws XbbException {
        return new HashMap<>(BasicConstant.DEFAULT_ACCURACY);
    }

    @Override
    public JSONObject returnSaasObject(String corpid, Integer businessType, UserVO userVO, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        return null;
    }

    @Override
    public List<TopPermissionsPoJo> getMobileDetailBottomOperation(HandelDetailPojo handelDetailPojo, PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        Integer businessType = handelDetailPojo.getBusinessType();
        Integer businessType4Distributor = XbbRefTypeEnum.transferBusinessType4Distributor(businessType, handelDetailPojo.getDistributorMark());
        UserVO userVO = handelDetailPojo.getLoginUser();
        Integer distributorMark = handelDetailPojo.getDistributorMark();
        List<TopPermissionsPoJo> bottomOperationList = new ArrayList<>();
        List<MobileDetailBottomOperationEnum> bottomOperationEnumList = MobileDetailBottomOperationEnum.getByBusinessTypeAndPermission(businessType4Distributor, userVO.getPermSet());
        String communicatePlanStatus = FastJsonHelper.getStringOrDefaultFromFormData(paasFormDataEntityExt.getData(), CommunicatePlanEnum.STATUS.getAttr(), CommunicatePlanStatusEnum.UNKNOW.getCode());
        for (MobileDetailBottomOperationEnum anEnum : bottomOperationEnumList) {
            if (Objects.equals(communicatePlanStatus, CommunicatePlanStatusEnum.FINISH.getCode()) && Objects.equals(anEnum, MobileDetailBottomOperationEnum.COMMUNICATE_PLAN_FINISH)) {
                continue;
            }
            if (Objects.equals(communicatePlanStatus, CommunicatePlanStatusEnum.CANCEL.getCode()) && Objects.equals(anEnum, MobileDetailBottomOperationEnum.COMMUNICATE_PLAN_CANCLE)) {
                continue;
            }
            TopPermissionsPoJo topPermissionsPoJo = new TopPermissionsPoJo();
            BeanUtil.copyProperties(anEnum, topPermissionsPoJo);
            bottomOperationList.add(topPermissionsPoJo);
        }
        return bottomOperationList;
    }

    @Override
    public void returnVO(PaasFormEntityExt formEntity, PaasFormDataEntityExt formData, MobileFormCommonDetailVO mobileFormCommonDetailVO, String userId) throws XbbException {
        mobileFormCommonDetailVO.setHasOuterLinkShare(setOuterLinkShare(formEntity.getCorpid(), formEntity.getId()));
        mobileFormCommonDetailVO.setDistributorMark(formEntity.getDistributorMark());
        mobileFormCommonDetailVO.setPaasFormEntity(formEntity);
    }

    @Override
    public List<ButtonPojo> handleDetailButton(FormDataDetailDTO formDataDetailDTO, HandelDetailPojo handelDetailPojo, PaasFormDataEntityExt paasFormDataEntityExt, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        Integer businessType = handelDetailPojo.getBusinessType();
        List<ButtonPojo> buttonPojoList = handelDetailPojo.getButtonPojoList();
        UserVO userVO = handelDetailPojo.getLoginUser();
        String corpid = formDataDetailDTO.getCorpid();
        List<BusinessDetailButtonEnum> buttonEnumList = BusinessDetailButtonEnum.getByBusinessTypeAndPermission(businessType, userVO.getPermSet());
        JSONObject dataList = paasFormDataEntityExt.getData();

        String planStatus = FastJsonHelper.getStringOrDefaultFromFormData(dataList, CommunicatePlanEnum.STATUS.getAttr(), "0");
        for (BusinessDetailButtonEnum buttonEnum : buttonEnumList) {
            if (Objects.equals(planStatus, CommunicatePlanStatusEnum.CANCEL.getCode()) && Objects.equals(buttonEnum, BusinessDetailButtonEnum.PLAN_QUIT)) {
                continue;
            } else if (Objects.equals(planStatus, CommunicatePlanStatusEnum.FINISH.getCode()) && Objects.equals(buttonEnum, BusinessDetailButtonEnum.PLAN_COMPLETE)) {
                continue;
            }
            ButtonPojo buttonPojo = new ButtonPojo();
            SaasButtonEnum saasButtonEnum = buttonEnum.getAnEnum();
            BeanUtil.copyProperties(saasButtonEnum, buttonPojo);
            buttonPojo.setLinkBusinessType(buttonEnum.getLinkBusinessType());
            buttonPojoList.add(buttonPojo);
        }
        cloudCodeHelp.setCloudCodeDetailButton(corpid, userVO.getDynamicPermSet(), paasFormDataEntityExt.getAppId(), paasFormDataEntityExt.getFormId(), businessType, buttonPojoList, formDataDetailDTO.getPlatform());
        return buttonPojoList;
    }


    @Override
    public List<TopPermissionsPoJo> dingtalkHandleDetailWorkFlow(HandelDetailPojo handelDetailPojo, PaasFormEntityExt formEntity, PaasFormDataEntityExt formData, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        String corpid = formEntity.getCorpid();
        UserVO userVO = handelDetailPojo.getLoginUser();
        Integer businessType = formEntity.getBusinessType();
        List<WorkFlowEnum> workFlowEnumList = WorkFlowEnum.getByBusinessTypeAndPermissionForDingtalk(businessType, userVO.getPermSet());
        List<String> aliasIn = new ArrayList<>();
        workFlowEnumList.forEach(item -> aliasIn.add(XbbRefTypeEnum.getByCode(item.getBusinessType()).getAlias()));
        Map<Integer, String> paasBusinessTypeToName = paasMenuModel.getMenuNameByAlias(aliasIn, corpid, businessType);

        List<TopPermissionsPoJo> topPermissions = businessButtonService.dingtalkDetailCommon(userVO, formEntity, explainMap, formData);
        List<LinkListPojo> linkList = formEntity.getLinkListList();
        topPermissions.addAll(linkListService.getDingtalkCommonButton(linkList, formData.getDel(), userVO, formData.getAppId(), formData.getFormId(), businessType, formEntity.getSaasMark(), corpid));

        for (WorkFlowEnum workFlowEnum : workFlowEnumList) {
            TopPermissionsPoJo topPermissionsPoJo = new TopPermissionsPoJo();
            BeanUtil.copyProperties(workFlowEnum, topPermissionsPoJo);
            if (Objects.equals(workFlowEnum.getFormRelation(), BasicConstant.ONE)) {
                NameUtil.changeValue(paasBusinessTypeToName, topPermissionsPoJo);
            }
            topPermissionsPoJo.setBusinessType(XbbRefTypeEnum.transferBusinessType2Index(workFlowEnum.getBusinessType()));
            topPermissionsPoJo.setDistributorMark(DistributorMarkEnum.OTHER.getCode());
            topPermissions.add(topPermissionsPoJo);
        }
        return topPermissions;
    }

    @Override
    public DataDetailPojo handleDetailData(Integer businessType, PaasFormDataEntityExt paasFormDataEntityExt, String userId, HandlerExplainDTO handlerExplainDTO) throws XbbException {
        DataDetailPojo dataDetailPojo = new DataDetailPojo();
        handlerExplainDTO.setCreatorId(paasFormDataEntityExt.getData().getString(CustomerManagementEnum.CREATOR.getAttr()));
        String corpid = handlerExplainDTO.getCorpid();
        Long formId = paasFormDataEntityExt.getFormId();

        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMapByList(handlerExplainDTO.getExplainList());
        // 呼叫中心权限
        boolean isActiveCall = callCenterService.isOpenCallAndAgent( corpid, userId);
        List<DetaPhonePojo> dial = new ArrayList<>();
        if ( isActiveCall ) {
            dial = callCenterService.cusCallPhone(paasFormDataEntityExt.getData(), businessType, corpid, explainMap, handlerExplainDTO);
        }
        // 呼叫中心号码
        dataDetailPojo.setDial(dial);
        //阿里云呼叫中心权限
        boolean isActiveAliyunCall = callCenterService.isActiveAliyunCall(corpid, userId).IsActiveAliyunAndHasRole();
        if ( isActiveAliyunCall ) {
            List<DetaPhonePojo> aliyunCallCenterDial = callCenterService.cusCallPhone(paasFormDataEntityExt.getData(), businessType, corpid, explainMap, handlerExplainDTO);
            if(CollectionUtils.isNotEmpty(aliyunCallCenterDial)&& StringUtil.isNotEmpty(aliyunCallCenterDial.get(0).getPhoneNum())){
                dataDetailPojo.setAliyunCallCenterDial(aliyunCallCenterDial);
            }
        }
        // 外链
        Integer outerLinkShare = setOuterLinkShare(corpid, formId);
        dataDetailPojo.setHasOuterLinkShare(outerLinkShare);
        return dataDetailPojo;
    }

    @Override
    public void handleDetailSpecialField(PaasFormDataEntityExt formData, JSONObject data, Map<String, FieldAttrEntity> explainMap, HandlerExplainDTO handlerExplainDTO) throws XbbException {

       JSONObject specialData = communicatePlanDetailAnalysisDataHandler.handleDetailSpecialField(formData, handlerExplainDTO.getBusinessType(), formData.getCorpid(), explainMap, formData.getFormId(), handlerExplainDTO);
       data.putAll(specialData);
    }


    @Override
    public JSONArray getSecondTitleList(FormDataDetailDTO formDataDetailDTO, PaasFormDataEntityExt paasFormDataEntityExt, PaasFormEntityExt formEntity, List<FieldAttrEntity> list, Map<String, FieldAttrEntity> explainMap, JSONObject data) throws XbbException {
        String corpid = formDataDetailDTO.getCorpid();
        Integer businessType = formDataDetailDTO.getBusinessType();
        Long formId = paasFormDataEntityExt.getFormId();
        List<SummaryDataPoJo> summaryList = getSummaryList(formDataDetailDTO, paasFormDataEntityExt, formEntity, list, explainMap, data);
        handelCommunicatePlanWithSecondTitle(corpid, paasFormDataEntityExt.getData(), summaryList, paasFormDataEntityExt);
        handleSummarySpecialAttr(corpid, summaryList, paasFormDataEntityExt);
        specialTransferKeyToShowValue(corpid, summaryList, businessType,data, formId);
        JSONArray secondTitle = getSecondTitleList(summaryList, data, formDataDetailDTO.getCorpid(), formEntity.getId());
        return secondTitle;

    }

    @Override
    public JSONArray getTagList(FormDataDetailDTO formDataDetailDTO, PaasFormDataEntityExt formData, PaasFormEntityExt formEntity, List<FieldAttrEntity> list, Map<String, FieldAttrEntity> explainMap, JSONObject data) throws XbbException {
        String corpid = formDataDetailDTO.getCorpid();
        Integer businessType = formEntity.getBusinessType();
        Long dataId = formData.getId();
        String userId = formDataDetailDTO.getUserId();
        Long formId = formData.getFormId();
        List<SummaryDataPoJo> labelsList = getLabelList(formDataDetailDTO, formData, formEntity, list, explainMap, data);
        ////访客计划延期的情况标签塞一个（访客计划状态）
        handleCommunicatePlanWithLabel(labelsList, formData.getData());
        handleSummarySpecialAttr(corpid, labelsList, formData);
        return getTags(labelsList, corpid, businessType, dataId, userId);
    }

    @Override
    public String getTitle(FormDataDetailDTO formDataDetailDTO, PaasFormDataEntityExt formData, PaasFormEntityExt formEntity, List<FieldAttrEntity> list, Map<String, FieldAttrEntity> explainMap, JSONObject data) throws XbbException {
        List<FormTitlePoJo> formTitleList = getTitleList(formDataDetailDTO, formData, formEntity, list, explainMap, data);
        return TitleUtil.getWebTitle(formTitleList);
    }

    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        if (loginUser.isMaxDataPermission()){
            return ;
        }
        Integer flowStatus = paasFormDataEntityExt.getFlowStatus();
        Long menuId = formEntity.getMenuId();
        Integer saasMark = formEntity.getSaasMark();
        Integer businessType = formEntity.getBusinessType();

        // 表单查看权限校验
        verifyQueryPermission(businessType, loginUser,saasMark ,menuId ,flowStatus);
        //团队权限&共享权限校验
        communicatePlanPermissionService.checkPermission(formEntity, paasFormDataEntityExt, loginUser, subBusinessType);

    }

    @Override
    public JSONObject analysisData(PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO, HandlerExplainVO handlerExplainVO) throws XbbException {
        Long formId = paasFormDataEntityExt.getFormId();
        List<FieldAttrEntity> list = handlerExplainVO.getHeadList();
        Integer businessType = handlerExplainDTO.getBusinessType();
        String corpid = handlerExplainDTO.getCorpid();
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(list.size());
        list.forEach(item->explainMap.put(item.getAttr(), item));

        ParseSingleRowDataVO parseSingleRowDataVO = commonAnalysisData(paasFormDataEntityExt, handlerExplainDTO, handlerExplainVO, list, explainMap);
        JSONObject data = parseSingleRowDataVO.getResult();
        handleDetailSpecialField(paasFormDataEntityExt, data,  explainMap,  handlerExplainDTO);
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
        BeanUtil.copyProperties(handlerExplainDTO, saasAnalysisDataDTO);
        saasAnalysisDataDTO.setFormId(formId);
        communicatePlanModel.formatData(saasAnalysisDataDTO, data);
        //处理千分位
        FormDataGetVO formDataGetVO = new FormDataGetVO();
        formDataGetVO.setData(data);
        formDataGetAnalysisDataHelp.analysisGetOfThousandBits(formDataGetVO, list, false);
        return data;
    }

    /**
     * 处理访客计划summaryList中需要特殊处理的字段
     *      * 重构前 -> com.xbongbong.saas.service.impl.MobileDetailServiceImpl#handleSummarySpecialAttr(java.lang.String, java.util.List, java.lang.Integer, com.alibaba.fastjson.JSONObject, java.lang.Long, com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt)
     * @param corpid
     * @param summaryList
     * @param paasFormDataEntityExt
     * @throws XbbException
     */
    private void handleSummarySpecialAttr(String corpid, List<SummaryDataPoJo> summaryList, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException{
        List<PaasFormDataEntityExt> childProductESList = new ArrayList<>();
        SummaryDataPoJo creatorSummary = FormUtil.getSummaryDataPoJoByAttr(summaryList, CommunicatePlanEnum.CREATOR_ID.getAttr());
        if (Objects.nonNull(creatorSummary) && Objects.nonNull(paasFormDataEntityExt)) {
            creatorSummary.setAttr(CommunicatePlanEnum.CREATOR_ID.getAttr());
            creatorSummary.setSaasAttr(CommunicatePlanEnum.CREATOR_ID.getSaasAttr());
            creatorSummary.setAttrName(CommunicatePlanEnum.CREATOR_ID.getAttrName());
            creatorSummary.setFieldType(CommunicatePlanEnum.CREATOR_ID.getFieldType());
            // 系统生成的访客计划（智能助手）
            String creatorName;
            if(Objects.equals(PaasConstant.SYS_USER_ID, paasFormDataEntityExt.getCreatorId())) {
                creatorName = I18nMessageUtil.getMessage(PaasConstant.SYS_GENERATED);
            } else {
                UserEntity userEntity = userModel.getByKey(paasFormDataEntityExt.getCreatorId(), corpid);
                creatorName = Objects.isNull(userEntity) ? "" : Objects.toString(userEntity.getName(), "");
            }
            creatorSummary.setValue(Collections.singletonList(creatorName));
        }
    }

    /**
     * Description: 处理访客计划延期标签问题
     * 重构前 -> com.xbongbong.saas.service.impl.MobileDetailServiceImpl#handleCommunicatePlan(java.util.List, com.alibaba.fastjson.JSONObject)
     * @param labelsList
     * @param data
     */
    private void handleCommunicatePlanWithLabel(List<SummaryDataPoJo> labelsList, JSONObject data) {
        if (data.containsKey(CommunicatePlanEnum.STATUS.getAttr())) {
            if (Objects.equals(data.get(CommunicatePlanEnum.STATUS.getAttr()), CommunicatePlanStatusEnum.DELAY.getCode())){
                SummaryDataPoJo poJo = new SummaryDataPoJo();
                poJo.setAttr(CommunicatePlanEnum.STATUS.getAttr());
                poJo.setSaasAttr(CommunicatePlanEnum.STATUS.getSaasAttr());
                poJo.setValue(Collections.singletonList(CommunicatePlanStatusEnum.DELAY.getAlias()));
                poJo.setAttrName(CommunicatePlanEnum.STATUS.getAttrName());
                poJo.setFieldType(CommunicatePlanEnum.STATUS.getFieldType());
                labelsList.add(poJo);
            } else if ((Objects.equals(data.get(CommunicatePlanEnum.STATUS.getAttr()), CommunicatePlanStatusEnum.NO_FOLLOW.getCode()))) {
                SummaryDataPoJo poJo = new SummaryDataPoJo();
                poJo.setAttr(CommunicatePlanEnum.STATUS.getAttr());
                poJo.setSaasAttr(CommunicatePlanEnum.STATUS.getSaasAttr());
                poJo.setValue(Collections.singletonList(CommunicatePlanStatusEnum.NO_FOLLOW.getAlias()));
                poJo.setAttrName(CommunicatePlanEnum.STATUS.getAttrName());
                poJo.setFieldType(CommunicatePlanEnum.STATUS.getFieldType());
                labelsList.add(poJo);
            } else if ((Objects.equals(data.get(CommunicatePlanEnum.STATUS.getAttr()), CommunicatePlanStatusEnum.FINISH.getCode()))) {
                SummaryDataPoJo poJo = new SummaryDataPoJo();
                poJo.setAttr(CommunicatePlanEnum.STATUS.getAttr());
                poJo.setSaasAttr(CommunicatePlanEnum.STATUS.getSaasAttr());
                poJo.setValue(Collections.singletonList(CommunicatePlanStatusEnum.FINISH.getAlias()));
                poJo.setAttrName(CommunicatePlanEnum.STATUS.getAttrName());
                poJo.setFieldType(CommunicatePlanEnum.STATUS.getFieldType());
                labelsList.add(poJo);
            } else if ((Objects.equals(data.get(CommunicatePlanEnum.STATUS.getAttr()), CommunicatePlanStatusEnum.CANCEL.getCode()))) {
                SummaryDataPoJo poJo = new SummaryDataPoJo();
                poJo.setAttr(CommunicatePlanEnum.STATUS.getAttr());
                poJo.setSaasAttr(CommunicatePlanEnum.STATUS.getSaasAttr());
                poJo.setValue(Collections.singletonList(CommunicatePlanStatusEnum.CANCEL.getAlias()));
                poJo.setAttrName(CommunicatePlanEnum.STATUS.getAttrName());
                poJo.setFieldType(CommunicatePlanEnum.STATUS.getFieldType());
                labelsList.add(poJo);
            }
        }
    }

    /**
     * 访客计划延期原因，延期备注
     * 重构前 -> com.xbongbong.saas.service.impl.MobileDetailServiceImpl#handelCommunicatePlan(java.lang.String, com.alibaba.fastjson.JSONObject, java.util.List, com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt)
     * @param corpid
     * @param data
     * @param summaryList
     * @param paasFormDataEntityExt
     * @throws XbbException
     */
    private void handelCommunicatePlanWithSecondTitle(String corpid, JSONObject data, List<SummaryDataPoJo> summaryList, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        if (data.containsKey(CommunicatePlanEnum.STATUS.getAttr()) && Objects.equals(data.get(CommunicatePlanEnum.STATUS.getAttr()), CommunicatePlanStatusEnum.DELAY.getCode())) {
            if (data.containsKey(CommunicatePlanEnum.DELAY_CAUSE.getAttr())) {
                SummaryDataPoJo summaryDataPoJo = new SummaryDataPoJo();
                String text = "";
                String delayCause = data.getString(CommunicatePlanEnum.DELAY_CAUSE.getAttr());
                List<ItemPoJo> dictionaryItemList = dataDictionaryModel.getDictionaryList4Saas(DictionaryEnum.DELAY_CAUSE, corpid, paasFormDataEntityExt.getFormId());
                for (ItemPoJo poJo : dictionaryItemList) {
                    if (Objects.equals(poJo.getValue().toString(), delayCause)) {
                        text = poJo.getText();
                    }
                }
                if (delayCause != null) {
                    summaryDataPoJo.setAttr(CommunicatePlanEnum.DELAY_CAUSE.getAttr());
                    summaryDataPoJo.setSaasAttr(CommunicatePlanEnum.DELAY_CAUSE.getSaasAttr());
                    summaryDataPoJo.setAttrName(CommunicatePlanEnum.DELAY_CAUSE.getAttrName());
                    summaryDataPoJo.setFieldType(CommunicatePlanEnum.DELAY_CAUSE.getFieldType());
                    summaryDataPoJo.setValue(Collections.singletonList(text));
                    summaryList.add(summaryDataPoJo);
                }
            }
            if (data.containsKey(CommunicatePlanEnum.DELAY_MEMO.getAttr())) {
                SummaryDataPoJo summaryDataPoJo = new SummaryDataPoJo();
                String delayMemo = data.getString(CommunicatePlanEnum.DELAY_MEMO.getAttr());
                if (delayMemo != null) {
                    summaryDataPoJo.setAttr(CommunicatePlanEnum.DELAY_MEMO.getAttr());
                    summaryDataPoJo.setSaasAttr(CommunicatePlanEnum.DELAY_MEMO.getSaasAttr());
                    summaryDataPoJo.setAttrName(CommunicatePlanEnum.DELAY_MEMO.getAttrName());
                    summaryDataPoJo.setFieldType(CommunicatePlanEnum.DELAY_MEMO.getFieldType());
                    summaryDataPoJo.setValue(Collections.singletonList(delayMemo));
                    summaryList.add(summaryDataPoJo);
                }
            }
        }
    }
}
